package edu.hhu.controller;

import com.jfinal.core.Controller;
import com.jfinal.ext.render.excel.PoiRender;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.auth.AccessTokenBuilder;

import edu.hhu.basic.table.CreateExcel;
import edu.hhu.dynamic.table.QueryExcel;

import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class PoiController extends Controller {

    public void createExcel() {
    	
		String nodeStr=this.getPara("nodeStr");
		nodeStr=nodeStr.substring(0, nodeStr.length()-1);
		
		String fieldStr=this.getPara("fieldStr");
		fieldStr=fieldStr.substring(0, fieldStr.length()-1);
		
		String s_date=this.getPara("s_date");
		String e_date=this.getPara("e_date");
		
//		System.out.println(nodeStr);
//		System.out.println(fieldStr);
//		System.out.println(s_date);
//		System.out.println(e_date);
        
        String path=this.getRequest().getSession().getServletContext().getRealPath("/");
        String filePath="files/"+nodeStr.split(",")[0]+"等井数据.xls";
//		System.out.println(path);
//		System.out.println(filePath);


        QueryExcel excel = new QueryExcel();
		try {
			excel.writeExcel(path+filePath,nodeStr,fieldStr,s_date,e_date);
		} catch (Exception e) {
			e.printStackTrace();

		}
		renderJson("success");
    }

    public void getExcel() {
    	String nodeStr=this.getPara("nodeStr");
		try{
			nodeStr = URLDecoder.decode(nodeStr, "UTF-8");
		}catch(Exception e){
			e.printStackTrace();
		}
		nodeStr=nodeStr.substring(0, nodeStr.length()-1);
//    	System.out.println(nodeStr);

        renderFile("/files/"+nodeStr.split(",")[0]+"等井数据.xls");
    }

public void exportExcel() {
    	
		String table=this.getPara("table");		
		System.out.println(table);
        String str = getPara("id");
        System.out.println(str);
        String check= getPara("choice");
        String[] split = str.split(",");
        
        String path=this.getRequest().getSession().getServletContext().getRealPath("/");
        String filePath="files1/"+table+".xls";
//		System.out.println(path);
//		System.out.println(filePath);
        

        CreateExcel excel = new CreateExcel();
		try {
			excel.writeExcel(path+filePath,table,split,check);
		} catch (Exception e) {
			e.printStackTrace();

		}
		renderJson("success");
    }


public void saveExcel() {
	String table=this.getPara("table");
	System.out.println(table);
//	System.out.println(nodeStr);

    renderFile("/files1/"+table+".xls");
}
}
